package com.ruoyi.api.worker;

import com.github.pagehelper.PageHelper;
import com.ruoyi.system.utils.TokenUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.JiajiaItem;
import com.ruoyi.system.domain.JiajiaWorker;
import com.ruoyi.system.domain.JiajiaWorkerItem;
import com.ruoyi.system.service.IJiajiaWorkerService;
import com.ruoyi.system.service.impl.JiajiaItemServiceImpl;
import com.ruoyi.system.service.impl.JiajiaWorkerItemServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/worker/item")
@Api(tags = "社工项目相关接口")
public class ApiWorkerItemController {

    @Autowired
    private JiajiaItemServiceImpl itemService;
    @Autowired
    private JiajiaWorkerItemServiceImpl workerItemService;

    @Autowired
    private IJiajiaWorkerService workerService;

    @ApiOperation(value = "社工展示的项目")
    @PostMapping("/myItemList")
    public AjaxResult myItemList() {

        JiajiaWorker worker = workerService.selectJiajiaWorkerById(TokenUtil.getUid());
        if (worker.getCodeAuth().intValue() != 1){
            return AjaxResult.error("请先进行身份认证");
        }

        JiajiaWorkerItem workerItem = new JiajiaWorkerItem();
        workerItem.setWid(TokenUtil.getUid());
        PageHelper.orderBy("wi.status desc");
        List<JiajiaWorkerItem> workerItemList = workerItemService.selectJiajiaWorkerItemList(workerItem);

        String iidsArr = StringUtils.listsToSbStr(workerItemList,"iid");

        JiajiaItem item = new JiajiaItem();
        item.getParams().put("idsArr",iidsArr.split(","));
        List<JiajiaItem> itemList = itemService.selectJiajiaItemList(item);

        Map<String,JiajiaItem> itemMap = StringUtils.listsToMap(itemList,"id");

        for (JiajiaWorkerItem model : workerItemList) {
            model.getParams().put("item","");
            if (itemMap.containsKey(model.getIid().toString())){
                model.getParams().put("item",itemMap.get(model.getIid().toString()));
            }
        }

        return AjaxResult.success(workerItemList);
    }

    @ApiOperation(value = "项目列表")
    @PostMapping("/itemList")
    public AjaxResult itemList() {

        JiajiaWorker worker = workerService.selectJiajiaWorkerById(TokenUtil.getUid());
        if (worker.getCodeAuth().intValue() != 1){
            return AjaxResult.error("请先进行身份认证");
        }

        JiajiaItem item = new JiajiaItem();
        List<JiajiaItem> itemList = itemService.selectJiajiaItemList(item);

        JiajiaWorkerItem workerItem = new JiajiaWorkerItem();
        workerItem.setWid(TokenUtil.getUid());
        List<JiajiaWorkerItem> workerItemList = workerItemService.selectJiajiaWorkerItemList(workerItem);

        Map<String, JiajiaWorkerItem> myItemMap = new HashMap<>();

        for (JiajiaWorkerItem model : workerItemList) {
            if (!myItemMap.containsKey(model.getIid().toString())) {
                myItemMap.put(model.getIid().toString(), model);
            }
        }
        for (JiajiaItem model : itemList) {
            model.getParams().put("isChoose", "false");
            if (myItemMap.containsKey(model.getId().toString())) {
                model.getParams().put("isChoose", "true");
            }
        }
        return AjaxResult.success(itemList);
    }

    @ApiOperation(value = "社工添加项目")
    @PostMapping("/addItem")
    public AjaxResult addItem(@RequestParam @ApiParam(value = "项目IDs") String itemIds) {

        JiajiaWorker worker = workerService.selectJiajiaWorkerById(TokenUtil.getUid());
        if (worker.getCodeAuth().intValue() != 1){
            return AjaxResult.error("请先进行身份认证");
        }
        for (String id : itemIds.split(",")) {
            JiajiaWorkerItem workerItem = new JiajiaWorkerItem();
            workerItem.setWid(TokenUtil.getUid());
            workerItem.setIid(Long.parseLong(id));
            List<JiajiaWorkerItem> workerItemList = workerItemService.selectJiajiaWorkerItemList(workerItem);
            if (workerItemList.isEmpty()){
                workerItemService.insertApiJiajiaWorkerItem(workerItem);
            }
        }

        return AjaxResult.success("添加成功");
    }

    @ApiOperation(value = "上架项目，下架项目")
    @PostMapping("/openOrCloseItem")
    public AjaxResult openOrCloseItem(@RequestParam @ApiParam(value = "社工项目ID") Long id) {

        JiajiaWorker worker = workerService.selectJiajiaWorkerById(TokenUtil.getUid());
        if (worker.getCodeAuth().intValue() != 1){
            return AjaxResult.error("请先进行身份认证");
        }

        JiajiaWorkerItem workerItem = workerItemService.selectJiajiaWorkerItemById(id);
        workerItem.setStatus(workerItem.getStatus()==0?1:0);
        workerItemService.updateJiajiaWorkerItem(workerItem);

        return AjaxResult.success("修改成功");
    }

    @ApiOperation("项目详情")
    @PostMapping("/itemDetail")
    public AjaxResult itemDetail(@RequestParam @ApiParam(value = "项目id") Long id){
        JiajiaItem item= itemService.selectJiajiaItemById(id);
        return AjaxResult.success(item);
    }



}
